Ext.define('AM.default.DefaultTree', {
    extend: 'Ext.tree.Panel',
    
    useArrows: true,
    rootVisible: false,
    initComponent: function(){
        this.store.load();
        this.addListener("itemclick", function(view, record, htmlelement, index, eventObject, object){
            // 4.0的树节点点击事件,非叶子节点默认打开,如果需要加入其他动作，请加入leafClick方法或者直接覆盖itemclick事件
            if (record.data.leaf) {
                if (this.leafClick != null) {
                    this.leafClick(view, record, htmlelement, index, eventObject, object);
                }
            }
            else {
                if (this.notLeafClick != null) {
                    this.notLeafClick(view, record, htmlelement, index, eventObject, object);
                }
                else {
                    this.openTree(view, record, htmlelement, index, eventObject, object);
                }
            }
        });
        this.callParent(arguments);
    },
    doLoad: function(){
        this.store.load();
    },
    // 叶子节点点击事件
    leafClick: null,
    // 默认非叶子节点点击事件为打开节点，不做其他操作，如需其他操作，需覆盖此方法
    notLeafClick: null,
    // 打开节点
    openTree: function(view, record, htmlelement, index, eventObject, object){
        var expand = record.get('expanded')
        if (expand) {
            view.collapse(record);
        }
        else {
            view.expand(record);
        }
    },
    // 在当前点击的节点下追加子节点
    insertChildNode: function(id, text, leaf, index){
        var record = this.getSelectionModel().getLastSelected();
        if (index) {
            // 创建至某个位置
            record.insertChild(index, {
                id: id,
                text: text,
                leaf: leaf
            });
        }
        else {
            // 创建子节点
            record.appendChild({
                id: id,
                text: text,
                leaf: leaf
            });
        }
        // 修改为非叶子节点
        if (record.data.leaf) {
            record.data.leaf = false;
        }
        // 展开当前节点
        this.view.expand(record);
    }
});
